package com.kerrykidz.system.controller;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.dao.model.LocPartyInoutEntity;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.dao.model.SysBarCodeEntity;
import com.kerrykidz.system.service.IReportService;
import com.kerrykidz.system.service.ISysBarCodeService;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.ReportSearchParam;
import com.rdp.framework.exception.RDPException;

@RequestMapping("SysBarCode")
@Controller
public class SysBarCodeController {
	@Autowired
	private IReportService reportService;
	@Autowired	
	private ISysBarCodeService iSysBarCodeService;
	@Autowired
	private ICommonService iCommonService;
	
	@RequestMapping("/system")
	public ModelAndView system() throws RDPException{
		String branId = UserUtil.getUserFromSession().getBranId();
		List<GblSecBranEntity> list = iSysBarCodeService.selectBranList(branId);
		ModelAndView mv = new ModelAndView("system/system_tiam");
		mv.addObject("list", list);
		mv.addObject("branId", branId);
		return mv;
	}
	@RequestMapping(value="selectSysBarCode", method=RequestMethod.POST)
	@ResponseBody
	public SysBarCodeEntity selectSysBarCode(SysBarCodeEntity entity) throws RDPException{
		
		return iSysBarCodeService.findBarCodeList(entity);
}
	
	
	
	@RequestMapping("/batch")
	@ResponseBody
	public String b1atch(String chestr,String status)throws RDPException{
		String modiBy = UserUtil.getUserFromSession().getUserId();
		SysBarCodeEntity barCodeEntity = new SysBarCodeEntity();
		barCodeEntity.setModiBy(modiBy);
		barCodeEntity.setStatus(status);
		String [] bcodeId = chestr.split(",");
		int length = bcodeId.length;
		List<String> bcodeList= iSysBarCodeService.findProcessInOut(bcodeId);
		String resultMsg="";
		boolean isDelete=true;
		if(null != bcodeList && bcodeList.size()>0){
			isDelete =false;
			for(int j=0;j<bcodeList.size();j++){
				if(j==0){
					resultMsg +=bcodeList.get(j);
				}else{
					if(j>1 && j%2==0){
						resultMsg +=","+bcodeList.get(j)+"<br/>";
					}else{
						resultMsg +=","+bcodeList.get(j);
					}
					
				}
			}
		}else{
			resultMsg ="success";
		}
		for(int i=0;i<length;i++){
			String delbcodeId =bcodeId[i];
			boolean okDelete =true;
			if(!isDelete){
				for(int j=0;j<bcodeList.size();j++){
					String noDelBcodeId =bcodeList.get(j);
					if(delbcodeId.equals(noDelBcodeId)){
						okDelete =false;
						break;
					}
				}
			}
			if(okDelete){
				barCodeEntity.setPkBcodeId(delbcodeId);
				iSysBarCodeService.deleteBcode(barCodeEntity);
			}
		}
		return resultMsg;
	
		}
	
	@RequestMapping("/saveBatch")
	@ResponseBody
	public String saveBatch(@RequestParam(value="branId") String branId
			, @RequestParam(value="saveCount") String saveCount)throws RDPException{
		SysBarCodeEntity barCodeEntity = iSysBarCodeService.selectMaxSBarCode();
		int cstart=0;
		 if(null !=barCodeEntity){
			 String barCode =barCodeEntity.getBarCode();
			 
			 cstart =Integer.parseInt(barCode.substring(6,barCode.length()));
		 }
		 int total =Integer.parseInt(saveCount) +cstart;
		List<SysBarCodeEntity> barCodeList = getbarCodeNo(cstart,total);
		Map<String,Object> savemap =new HashMap<String,Object>();
		savemap.put("branId", branId);
		savemap.put("barCodeList", barCodeList);
		savemap.put("modiBy", UserUtil.getUserFromSession().getUserId());
			iSysBarCodeService.saveBatch(savemap);
		return"success";
	
		}
	
	public static List<SysBarCodeEntity> getbarCodeNo(int ajNos,int count){
		String ajNo="";
		int kstart=ajNos +1;
		count =count+1;
		List<SysBarCodeEntity> ajNoList =new ArrayList<SysBarCodeEntity>();
		String nowDateStr =DateUtil.formatDate("yyMMdd", new Date());
			for(int k=kstart;k<count;k++){
				ajNo =""; 
				DecimalFormat df=new DecimalFormat("000000");   
				ajNo=df.format(k);
		    	SysBarCodeEntity sysBarCode =new SysBarCodeEntity();
		    	sysBarCode.setBarCode(nowDateStr+ajNo);
		    	String pkBcodeId = PrimaryKeyGenerator.getRandomPrimaryKey();
		    	sysBarCode.setPkBcodeId(pkBcodeId);
		    	ajNoList.add(sysBarCode);
			}
		return ajNoList;
	}
	 
	/**
	 * 导出条码Excel
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value="/downLoadBcode",method=RequestMethod.POST)
	public void downLoadBcode(SysBarCodeEntity entity,ReportSearchParam reportParam,HttpSession session,HttpServletResponse response)throws RDPException{
		String branId = UserUtil.getUserFromSession().getBranId();
		entity.setFkBranId(branId);
		List<SysBarCodeEntity> list = iSysBarCodeService.selectBarCodeList(entity);
		if(list == null){
			return;
		}
		reportParam.setList(list);
		reportParam.setReportName("tiaoma");
		reportService.barCodeExport(reportParam, response);
	}
	
	 
	/**
	 * 批量导出条码Excel
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value="/downLoadBcode1",method=RequestMethod.POST)
	@ResponseBody
	public String downLoadBcode1(String chestr,ReportSearchParam reportParam,HttpServletResponse response)throws RDPException{
		try {
			List<SysBarCodeEntity> list = new ArrayList<SysBarCodeEntity>();
			String[] bcodeId = chestr.split(",");
			int length = bcodeId.length;
			SysBarCodeEntity entity = new SysBarCodeEntity();
			for (int i = 0; i < length; i++) {
				entity.setPkBcodeId(bcodeId[i]);
				SysBarCodeEntity entity1 = iSysBarCodeService
						.selectBarCode(entity);
				list.add(entity1);
			}
			reportParam.setList(list);
			reportParam.setReportName("tiaoma");
			reportService.barCodeExport(reportParam, response);
			return Constants.SUCCESS;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}
}
